import userStore from '@/store/user';
import tabbarStore from '@/store/tabbar';
import { createStoreBindings } from '@/miniprogram_npm/mobx-miniprogram-bindings/index';

const modules = [
    {
        name: 'weather',
        text: '天气',
        icon: '/static/icon/weather.svg',
        page: '/module/pages/weather/index'
    },
    {
        name: 'electricity',
        text: '电力',
        icon: '/static/icon/electricity.svg',
        page: '/module/pages/electricity/index'
    },
    {
        name: 'stock',
        text: '股票',
        icon: '/static/icon/stock.svg',
        page: '/module/pages/stock/index'
    }
];

const functions = [
    {
        name: 'introduction',
        text: '模型介绍',
        icon: 'book-open',
        page: '/info/pages/introduction/index'
    },
    {
        name: 'knowledge',
        text: '知识空间',
        icon: 'course',
        page: '/info/pages/knowledge/index'
    },
    {
        name: 'predict',
        text: '数据预测',
        icon: 'control-platform',
        page: '/pages/predict/pages/index/index'
    },
    {
        name: 'history',
        text: '预测历史',
        icon: 'history',
        page: '/pages/history/index/index'
    }
];

const others = [
    {
        name: 'feedback',
        text: '问题反馈',
        icon: 'info-circle'
    }
];

Page({
    data: {
        modules,
        functions,
        others
    },

    onLoad() {
        this.storeBindings = createStoreBindings(this, {
            store: userStore,
            fields: ['userInfo']
		});
    },

    onUnload() {
        this.storeBindings.destroyStoreBindings();
    },

    jumpToModule(e) {
        wx.navigateTo({
            url: modules[e.currentTarget.dataset.index].page
        });
    },

    jumpToFunction(e) {
        const func = functions[e.currentTarget.dataset.index];
        if (func.name === 'predict') {
            tabbarStore.setCurrent('predict');
            wx.switchTab({
                url: func.page
            });
            return;
        }
        wx.navigateTo({
            url: functions[e.currentTarget.dataset.index].page
        });
    },

    jumpToOther(e) {
        switch (e.currentTarget.dataset.name) {
            case 'personal': {
                wx.navigateTo({
                    url: '/pages/user/user'
                });
                break;
            }
            case 'feedback': {
                wx.navigateTo({
                    url: '/pages/feedback/index'
                });
                break;
            }
        }
    }
});
